import { NextRequest } from 'next/server';
import { ResponseUtil } from '@/utils/response';
import { verifyAuth } from '@/utils/auth';
import prisma from '@/lib/prisma';

export async function GET(request: NextRequest) {
  try {
    // 验证用户登录
    const authResult = await verifyAuth(request);
    if (!authResult?.user?.id) {
      return ResponseUtil.unauthorized('未登录');
    }

    const userId = authResult.user.id;

    // 获取用户信息
    const user = await prisma.user.findUnique({
      where: { id: userId },
      select: {
        id: true,
        nickname: true,
        phone: true,
        email: true,
        avatar: true,
        points: true,
        withDrawPoints: true // 直接使用可提现积分字段
      }
    });

    if (!user) {
      return ResponseUtil.notFound('用户不存在');
    }

    return ResponseUtil.success({
      id: user.id,
      nickname: user.nickname,
      phone: user.phone,
      email: user.email,
      avatar: user.avatar,
      totalPoints: user.points, // 总积分
      withdrawablePoints: user.withDrawPoints // 可提现积分
    });

  } catch (error) {
    console.error('获取可提现积分失败:', error);
    return ResponseUtil.serverError('获取可提现积分失败');
  }
} 